/* This is the client portion of program 2 */

#include "getKey.h"
#include "sgf.h"


void email_cli(int sockfd, char *key)
{

    //receive buffer
    char recv_buffer[100]; 
    char dtext[100];
    //send buffers
    char username[13], password[8], selection[1];
    char e_username[50], e_password[50], e_mssg_num[50], e_selection[50];

    //send int
    int mssg_num;
    char ch_mssg_num[4];

    mssg_num = 0;
    ch_mssg_num[0] = '0';
    //clear recv_buffer
    bzero(recv_buffer, sizeof(recv_buffer));
    // recv welcome message
    // Readn(sockfd, recv_buffer, 43);
    Readn(sockfd, recv_buffer, 48);
    sgfd(key, recv_buffer, CTEXTLENGTH(43), dtext); /* decrypt */

    // print welcome message to screen
    printf("%s",dtext);

    //clear recv_buffer
    bzero(recv_buffer, sizeof(recv_buffer));
    bzero(dtext, sizeof(dtext));
    // recv login prompt message
    //Readn(sockfd, recv_buffer, 7);
    Readn(sockfd, recv_buffer, 16);
    sgfd(key, recv_buffer, CTEXTLENGTH(7), dtext);
    // print login prompt to screen
    printf("%s",dtext);

    //read username from screen
    scanf("%s",username);
    bzero(e_username, sizeof(e_username));
    sgfe(key, username, e_username);
    // send username

    Writen(sockfd, e_username, CTEXTLENGTH(strlen(e_username)));

    //clear recv_buffer
    bzero(recv_buffer, sizeof(recv_buffer));
    bzero(dtext, sizeof(dtext));
    // recv password prompt message from server
    //Readn(sockfd, recv_buffer, 10);
    Readn(sockfd, recv_buffer, 16);
    sgfd(key, recv_buffer, CTEXTLENGTH(10), dtext);
    // print password prompt to screen
    printf("%s",dtext);

    //read password from screen
    scanf("%s",password);
    bzero(e_password, sizeof(e_password));
    sgfe(key, password, e_password);
    // send password to server
    Writen(sockfd, e_password, CTEXTLENGTH(strlen(e_password)));

    do {
	// read number of messages string from server (or password)
	//clear recv_buffer
	bzero(recv_buffer, sizeof(recv_buffer));
	bzero(dtext, sizeof(dtext));
	// recv number of messages string from server  
	Readn(sockfd, recv_buffer, CTEXTLENGTH(38));
	sgfd(key, recv_buffer, CTEXTLENGTH(38), dtext);
	// print number of messages string prompt to screen
	printf("%s",dtext);

	//enter invalid password - restart
	if (bcmp(dtext, "Error: Invalid login or password!!!!!\n",38) == 0 ) exit(0); 

	// read "Enter message number:" string from server
	//clear recv_buffer
	bzero(recv_buffer, sizeof(recv_buffer));
	bzero(dtext, sizeof(dtext));
	// recv "Enter message number:" string from server  
	Readn(sockfd, recv_buffer, CTEXTLENGTH(22));
	sgfd(key, recv_buffer, CTEXTLENGTH(22), dtext);
	// print "Enter message number:" prompt to screen
	printf("%s",dtext);


	//read message number from screen
	scanf("%d",&mssg_num);
	sprintf(ch_mssg_num,"%d", mssg_num);
	// send mssgnum to server
	bzero(e_mssg_num, sizeof(e_mssg_num));
	sgfe(key, ch_mssg_num, e_mssg_num);
	Writen(sockfd, e_mssg_num, CTEXTLENGTH(strlen(e_mssg_num)));


	// read email message string from server
	//clear recv_buffer
	bzero(recv_buffer, sizeof(recv_buffer));
	bzero(dtext, sizeof(dtext));
	// recv email message string from server 
	Readn(sockfd, recv_buffer, CTEXTLENGTH(18));
	sgfd(key, recv_buffer, CTEXTLENGTH(18), dtext);
	// print email message prompt to screen
	printf("\n%s",dtext);


	// read another message string from server
	//clear recv_buffer
	bzero(recv_buffer, sizeof(recv_buffer));
	bzero(dtext, sizeof(dtext));
	// recv email message string from server 
	Readn(sockfd, recv_buffer, CTEXTLENGTH(46));
	sgfd(key, recv_buffer, CTEXTLENGTH(46), dtext);
	// print email message prompt to screen
	printf("\n%s",dtext);

	//read message number from from screen
	scanf("%s",selection);
	bzero(e_selection, sizeof(e_selection));
	// send msgnum to server
	sgfe(key, selection, e_selection);
	Writen(sockfd, e_selection, CTEXTLENGTH(strlen(e_selection)));

    } while (selection[0] == 'y');


    // read goodbye message string from server
    //clear recv_buffer
    bzero(recv_buffer, sizeof(recv_buffer));
    bzero(dtext, sizeof(dtext));
    // recv goodbye message string from server 
    Readn(sockfd, recv_buffer, CTEXTLENGTH(7));
    sgfd(key, recv_buffer, CTEXTLENGTH(7), dtext);
    // print goodbye message prompt to screen
    printf("%s\n",dtext);
}


int main(int argc, char **argv)
{

    int			sockfd;
    struct sockaddr_in	servaddr;
#define     SERV_PORT        6225

    if (argc != 2)
	err_sys("usage: tcpcli <IPaddress>");
    int KDC_port = 32000;
    char* key = getKey(KDC_SERV_IP, KDC_port);
    printf("the recv key is %s\n", key);
    sockfd = Socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(SERV_PORT);
    inet_aton(argv[1], &servaddr.sin_addr);

    Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));
    printf("Connected to server at %s on %d......\n",argv[1], SERV_PORT);        
    email_cli(sockfd, key);		/* do it all */
    free(key);
    exit(0);
}

